package main.carteleracm.ca.utils;

import main.carteleracm.ca.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MyGalleryAdapter extends BaseAdapter {
	int defaultItemBackground;
	private Context galleryContext;
	private Bitmap[] imageBitmaps;
	Bitmap placeholder;
	Context context;
	
	public MyGalleryAdapter(Context c,Bitmap[] imgs) {
		context = c;
	    galleryContext = c; 
	    imageBitmaps  = new Bitmap[imgs.length];    	    
	    for(int i=0; i<imgs.length; i++)
	    	imageBitmaps[i] = imgs[i];
	    TypedArray styleAttrs = galleryContext.obtainStyledAttributes(R.styleable.MyGallery);
	    defaultItemBackground = styleAttrs.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
	    styleAttrs.recycle();
	}

	public int getCount() {
		return imageBitmaps.length;
	}
	public Object getItem(int position) {
		return position;
	}
	public long getItemId(int position) {
		return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
	    ImageView imageView = new ImageView(galleryContext);
	    imageView.setImageBitmap(imageBitmaps[position]);
	    int[] dim = Utils.getScreenDimensions(context, imageView);	    
	    imageView.setLayoutParams(new Gallery.LayoutParams(dim[0], dim[1]));
	    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
	    imageView.setBackgroundResource(defaultItemBackground);
	    return imageView;
	}	
}